/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thread;

import DLL.InitializationBroadCastGroup;
import DLL.ResponseFunction;
import java.net.DatagramPacket;
import java.net.InetAddress;
import javax.swing.*;

/**
 *
 * @author PyyPyLyn
 */
public class ListenBroadcast_Thread extends Thread
{
    String FileName;
    InitializationBroadCastGroup b_Group;
    JLabel lbGoiTin;
    public ListenBroadcast_Thread(String fn, InitializationBroadCastGroup group, JLabel lb) 
    {
        FileName = fn;
        b_Group = group;
        lbGoiTin = lb;
    }

    
    public void run() 
    {
        try
        {
            byte[] data = new byte[1024];
            DatagramPacket packet = new DatagramPacket(data, data.length);
            while(true)
            {
                //b_Group.getM_Group().connect(b_Group.getM_Group().getLocalSocketAddress());
                
                b_Group.getM_Group().receive(packet) ;  
                //b_Group.getM_Group().close();
                if(packet.getAddress() == InetAddress.getLocalHost())
                {
                    return;
                }
                if(packet.getData()[0] == 0)
                {
                    JOptionPane.showMessageDialog(null, "Timeout");
                }
                lbGoiTin.setText("Đã Nhận Gói Tin Yêu Cầu");
                if(packet.getAddress() != InetAddress.getLocalHost())
                {
                    ResponseFunction.ResponseBroadcast(packet);
                }
            }
            
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Loi!");
        }
    }
    
    

    
}
